<template>
  <el-main>
    <!--搜索栏 -->
    <el-form :model="listParm" :inline="true" size="small">
      <el-form-item>
        <el-select style="width:140px;" v-model="listParm.categoryId" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 136px;" placeholder="请输入图书名称" v-model="listParm.bookName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 136px;" placeholder="请输入书架号" v-model="listParm.bookPlaceNum"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 136px;" placeholder="请输入ISBN号" v-model="listParm.bookCode"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input style="width: 136px;" placeholder="请输入作者" v-model="listParm.bookAuther"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round icon="el-icon-search" @click="searchBtn">查询</el-button>
        <el-button type="danger" round icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:bookList:add']" type="success" round icon="el-icon-plus" @click="addBtn">新增
        </el-button>
        <el-button v-permission="['sys:bookList:export']" type="primary" round icon="el-icon-top" @click="exportBtn">
          导出
        </el-button>
        <!--        导入前请先点击导出!使用导出的模板除了灰色标题以外,其他的可以自己修改。如果忽视本提示,导入将会出现问题!-->
        <el-tooltip
          content="导入前请先点击导出!使用导出的模板除了灰色标题以外,其他的可以自己修改。如果忽视本提示,导入将会出现问题!"
          placement="top"
          transition="el-zoom-in-center"
        >
          <el-upload
            action="http://localhost:8080/api/books/import"
            :show-file-list="false"
            accept="xlsx"
            style="margin-left:10px;display: inline-block"
            :on-success="importSuccess"
          >
            <el-button v-permission="['sys:bookList:import']" type="primary" round icon="el-icon-bottom">导入</el-button>
          </el-upload>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :height="tableHeight" :data="tableData" :default-sort="{prop:'tableData','order':'descending',}" border
              stripe
    >
      <el-table-column sortable label="图书名称" prop="bookName"></el-table-column>
      <el-table-column sortable label="图书分类" prop="categoryName"></el-table-column>
      <el-table-column sortable label="ISBN" prop="bookCode"></el-table-column>
      <el-table-column sortable label="书架号" prop="bookPlaceNum"></el-table-column>
      <el-table-column sortable label="作者" prop="bookAuther"></el-table-column>
      <el-table-column sortable label="出版社" prop="bookProduct"></el-table-column>
      <el-table-column sortable label="价格" prop="bookPrice"></el-table-column>
      <el-table-column sortable label="库存" prop="bookStore"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            v-permission="['sys:bookList:edit']"
            icon="el-icon-edit"
            type="primary"
            size="small"
            @click="editBtn(scope.row)"
          >编辑
          </el-button>
          <el-button
            v-permission="['sys:bookList:delete']"
            type="danger"
            icon="el-icon-delete"
            size="small"
            @click="deleteBtn(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="listParm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="listParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParm.total"
      background
    >
    </el-pagination>
    <!-- 新增弹框 -->
    <sys-dialog
      :title="dialog.title"
      :height="dialog.height"
      :width="dialog.width"
      :visible="dialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form
          :model="addModel"
          ref="addRef"
          :rules="rules"
          label-width="80px"
          size="small"
          style="margin-right: 10px"
        >
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="categoryId" label="图书分类">
                <el-select v-model="addModel.categoryId" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.categoryId"
                    :label="item.categoryName"
                    :value="item.categoryId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookName" label="图书名称">
                <el-input v-model="addModel.bookName"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookCode" label="图书编码">
                <el-input v-model="addModel.bookCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookPlaceNum" label="书架号">
                <el-input v-model="addModel.bookPlaceNum"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookAuther" label="图书作者">
                <el-input v-model="addModel.bookAuther"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookProduct" label="出版社">
                <el-input v-model="addModel.bookProduct"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookPrice" label="图书价格">
                <el-input v-model="addModel.bookPrice"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="bookStore" label="图书库存">
                <el-input v-model="addModel.bookStore"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>
<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import { getListApi, getCateListApi, addBookApi, editBookApi, deleteBookApi, exportBookApi } from '@/api/book'

export default {
  components: {
    SysDialog,
  },
  data () {
    return {
      //表单验证规则
      rules: {
        categoryId: [
          {
            required: true,
            trigger: 'blur',
            message: '请选择图书分类',
          },
        ],
        bookCode: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写图书编码',
          },
        ],
        bookPlaceNum: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写书架号',
          },
        ],
        bookProduct: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写图书出版社',
          },
        ],
        bookAuther: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写作者',
          },
        ],
        bookPrice: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写价格',
          },
        ],
        bookStore: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写库存',
          },
        ],
        bookName: [
          {
            required: true,
            trigger: 'blur',
            message: '请填写图书名称',
          },
        ],
      },
      //表单绑定的数据
      addModel: {
        bookId: '',
        categoryId: '',
        bookName: '',
        categoryName: '',
        bookCode: '',
        bookPlaceNum: '',
        bookProduct: '',
        bookAuther: '',
        bookPrice: '',
        bookStore: '',
        type: '',
      },
      //弹框属性
      dialog: {
        height: 250,
        width: 650,
        visible: false,
        title: '',
      },
      //下拉数据
      options: [],
      //表格高度
      tableHeight: 0,
      //表格数据
      tableData: [],
      //列表查询参数
      listParm: {
        currentPage: 1,
        pageSize: 10,
        categoryId: '',
        bookName: '',
        bookPlaceNum: '',
        bookAuther: '',
        bookCode: '',
        total: 0,
      },
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 207
    })
  },
  created () {
    this.getCateList()
    this.getList()
  },
  methods: {
    //导入成功
    importSuccess () {
      this.$message({ type: 'success', message: '文件导入成功!' })
      this.getList()
    },
    //导出excel
    async exportBtn () {
      if (window.open('http://localhost:8080/api/books/export')) {
        this.$message({ type: 'success', message: '导出成功!请下载!' })
      }
    },
    //获取列表
    async getList () {
      let res = await getListApi(this.listParm)
      if (res && res.code == 200) {
        this.tableData = res.data.records
        this.listParm.total = res.data.total
      }
    },
    //弹框确定事件
    onConfirm () {
      this.$refs.addRef.validate(async(valid) => {
        if (valid) {
          let res = null
          if (this.addModel.type == '0') {
            res = await addBookApi(this.addModel)
          } else {
            res = await editBookApi(this.addModel)
          }
          if (res && res.code == 200) {
            //信息提示
            this.$message({ type: 'success', message: res.msg })
            //刷新表格
            this.getList()
            this.dialog.visible = false
          }
        }
      })
    },
    //新增弹框关闭
    onClose () {
      this.dialog.visible = false
    },
    //查询分类列表
    async getCateList () {
      let res = await getCateListApi()
      if (res && res.code == 200) {
        this.options = res.data
      }
    },
    //页数改变时触发
    currentChange (val) {
      this.listParm.currentPage = val
      this.getList()
    },
    //页容量改变时触发
    sizeChange (val) {
      this.listParm.pageSize = val
      this.getList()
    },
    //删除按钮
    async deleteBtn (row) {
      let confirm = await this.$myconfirm('确定删除该数据吗?')
      if (confirm) {
        let res = await deleteBookApi({ bookId: row.bookId })
        if (res && res.code == 200) {
          //信息提示
          this.$message({ type: 'success', message: res.msg })
          //刷新表格
          this.getList()
        }
      }
    },
    //编辑按钮
    editBtn (row) {
      this.dialog.title = '编辑图书'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //把当前要编辑的数据复制到表单绑定的数据域里面
      this.$objCoppy(row, this.addModel)
      this.addModel.type = '1'
    },
    //新增按钮
    addBtn () {
      this.dialog.title = '新增图书'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      this.addModel.type = '0'
    },
    //重置按钮
    resetBtn () {
      this.listParm.categoryId = ''
      this.listParm.bookName = ''
      this.listParm.bookPlaceNum = ''
      this.listParm.bookAuther = ''
      this.getList()
    },
    //搜索按钮
    searchBtn () {
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
</style>
